using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.ApplicationServer.Caching;
using System.Net;
using WCFServiceWebRole1.Pusher;

namespace WCFServiceWebRole1
{
    public class WebRole : RoleEntryPoint
    {
        public const string APIKEYLIST = "APIKeyList";
        public const string SUBSCRIBER_LIST = "SubscriberList";
        public const string SUBSCRIBED_SENSORS = "SubscribedSensors";
        public static DataCache myCache;
        public static DataCache getCacheObject()
        {

            // Caching Code
            // Create a DataCacheFactoryConfiguration object
            DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();

            // Enable the AutoDiscorveryProperty (and any other required configuration settings):
            config.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, "WCFServiceWebRole1");

            // Create a DataCacheFactory object with the configuration settings:
            DataCacheFactory factory = new DataCacheFactory(config);

            // Use the factory to create a DataCache client for the "default" cache:
            myCache = factory.GetCache("default");

            return myCache;

        }
        public override bool OnStart()
        {

            //// To enable the AzureLocalStorageTraceListner, uncomment relevent section in the web.config  
            //DiagnosticMonitorConfiguration diagnosticConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
            //diagnosticConfig.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
            //diagnosticConfig.Directories.DataSources.Add(AzureLocalStorageTraceListener.GetLogDirectory());

            //#region Diagnostics configuration

            //// Get the default initial configuration for DiagnosticMonitor.
            //DiagnosticMonitorConfiguration diagnosticConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration();


            //// Create a custom logging path for crash dumps.
            //string customCrashDumpsPath = RoleEnvironment.GetLocalResource("MyCustomCrashDumpsLocation").RootPath;


            //// Enable full crash dump collection to the custom path. Pass in false
            //// to enable mini crash dump collection. 
            //CrashDumps.EnableCollectionToDirectory(customCrashDumpsPath, true);


            //// Create a new DirectoryConfiguration object.
            //DirectoryConfiguration directoryConfiguration = new DirectoryConfiguration();


            //// Add the name for the blob container in Windows Azure storage.
            //directoryConfiguration.Container = "wad-crash-dumps";


            //// Add the directory size quota.
            //directoryConfiguration.DirectoryQuotaInMB = RoleEnvironment.GetLocalResource("MyCustomCrashDumpsLocation").MaximumSizeInMegabytes;


            //// Add the crash dumps path.
            //directoryConfiguration.Path = customCrashDumpsPath;


            //// Schedule a transfer period of 30 minutes.
            //diagnosticConfiguration.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(30.0);


            //// Add the directoryConfiguration to the Directories collection.
            //diagnosticConfiguration.Directories.DataSources.Add(directoryConfiguration);


            //// Start the DiagnosticMonitor using the diagnosticConfig and our connection string.
            //DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", diagnosticConfiguration);

            //#endregion

//            #region Cache Initialization

//            List<Guid> keyList = new List<Guid>();
//            Guid dummykey = new Guid("1e1db4ce-373b-4dac-9756-7b3d864fe9c9");
//            keyList.Add(dummykey);
//            myCache = WebRole.getCacheObject();
//            myCache.Add(APIKEYLIST, keyList);

//            //Creating Pusher subscription List
//            List<SubscriberRecord> subList = new List<SubscriberRecord>();
//            SubscriberRecord dummysub = new SubscriberRecord() { sensorid = 1, userid = 1 };
//            subList.Add(dummysub);
//          //  myCache = WebRole.getCacheObject();
//            myCache.Add(SUBSCRIBER_LIST, subList);

//            //Creating Subscribed Sensors List
//            List<int> sensorList = new List<int>();
//            sensorList.Add(1);
//            myCache.Add(SUBSCRIBED_SENSORS, sensorList);

//#endregion
            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }

     
    }
}
